<?php
namespace app\api\controller;
use app\common\model\School as SchoolModel;
use app\admin\model\Config;
use think\Db;
use app\common\service\GuguService;


class BatchQuery extends App
{
   
	
	
	//提前批
	public function tiqian()
    {
		$pagesize = 50;
        $page = $this->request->get("page",1,'intval');
		$province_id = $this->province_id;
		$user_id = $this->user_id;
        if(empty($province_id)){
            return $this->show(2001,"缺少参数province_id!", '');
        }
		
		$school_id = $this->request->get("school_id",0,'intval'); 
        if(!empty($school_id)){
            $college_name = Db::name("school")->where('id',$school_id)->value('college_name');
			$map['school_name'] = $college_name;
        }else{
			$college_name = '';
		}
		
		$year = date('Y',strtotime('-1 year'));
		$member = Db::name("member")->field('province_id,cj_score,cj_subjects1,cj_subjects2,cj_subjects,cj_subjects,cj_subject_mode_title')->where('id',$user_id)->find();
		$type_name = $member['cj_subject_mode_title'];
		//$batch_name = '本科提前批B段';
		$batch_name = SchoolModel::getTiqianBtachs();
		//$enrollprovince = Db::name("area")->where('id',$province_id)->value('name');
        
		$map['status'] = 1;
        $map['year'] = $year;
        $map['type'] = $type_name;
		$list = Db::name("school_plan_new")->field('college_major_name as major_name,batch_name,course_selection_requirements as selection,enrollment_numbers as people_nums,in_school_years as xuezhi')->where('province_id',$province_id)->where('school_name',$college_name)->where('batch_name','in',$batch_name)->where($map)->page($page,$pagesize)->select();
		//echo Db::name("school_plan_new")->getlastsql();die;
		$count = Db::name("school_plan_new")->field('college_major_name as major_name,batch_name,course_selection_requirements as selection,enrollment_numbers as people_nums,in_school_years as xuezhi')->where('province_id',$province_id)->where('school_name',$college_name)->where('batch_name','in',$batch_name)->where($map)->count();
        $pages = ceil($count/$pagesize);
		//dump($result);die;
		
        $data = ['list'=>$list,'count'=>$count,'pages'=>$pages,'pagesize'=>$pagesize];
		return $this->show(1,"成功", $data);
		
    }
	
	public function tiqian_cate_one()
    {
		
		$province_id = $this->province_id;
		$user_id = $this->user_id;
        if(empty($province_id)){
            return $this->show(2001,"缺少参数province_id!", '');
        }
		$year = date('Y',strtotime('-1 year'));
        $map['year'] = $year;
		
		
		$list = Db::name("school_plan_new")->field('type as title')->where('province_id',$province_id)->where($map)->group('type')->select();
		//SELECT TYPE , count( * ) asnums FROM `think_school_plan_new`WHERE year =2024 GROUP BY `type`
		return $this->show(1,"成功", $list);
		
    }
	public function tiqian_cate_two()
    {
		
		$province_id = $this->province_id;
		$user_id = $this->user_id;
        if(empty($province_id)){
            return $this->show(2001,"缺少参数province_id!", '');
        }
		$cate_title = $this->request->get("cate_title",'','trim'); 
        if(empty($cate_title)){
            return $this->show(2001,"缺少参数cate_title!", '');
        }
		$year = date('Y',strtotime('-1 year'));
        $map['year'] = $year;
		
		//SELECT batch_name, count( * ) AS nums FROM `think_major_line_new` WHERE year =2024 AND type_name = '体育文' GROUP BY `batch_name`
		$list = Db::name("school_plan_new")->field('batch_name as title')->where('province_id',$province_id)->where($map)->where('type',$cate_title)->group('batch_name')->select();
		return $this->show(1,"成功", $list);
		
    }
	
	//提前批
	public function tiqian_new()
    {
		$pagesize = 10;
        $page = $this->request->get("page",1,'intval');
		$province_id = $this->province_id;
		$user_id = $this->user_id;
        if(empty($province_id)){
            return $this->show(2001,"缺少参数province_id!", '');
        }
		
		$cate_title1 = $this->request->get("cate_title1",'','trim'); 
        if(!empty($cate_title1)){
            $map['type'] = $cate_title1;
        }
		$cate_title2 = $this->request->get("cate_title2",'','trim'); 
        if(!empty($cate_title)){
            $map['batch_name'] = $cate_title2;
        }
		
		$school_id = $this->request->get("school_id",0,'intval'); 
        if(!empty($school_id)){
            $college_name = Db::name("school")->where('id',$school_id)->value('college_name');
			$map['school_name'] = $college_name;
        }
		
		$year = date('Y',strtotime('-1 year'));
        $map['year'] = $year;
		$map['status'] = 1;
		
		$list = Db::name("school_plan_new")->field('school_name,special_group_name')->where($map)->page($page,$pagesize)->group('school_name,special_group_name')->select();
		//echo Db::name("school_plan_new")->getlastsql();die;
		$count = Db::name("school_plan_new")->where('province_id',$province_id)->where($map)->group('school_name,special_group_name')->count();
        $pages = ceil($count/$pagesize);
		
		$school_names = array_column($list,'school_name');
		$map1 = $map;
		$map1['school_name'] = array('in',$school_names);
		$major_list = Db::name("school_plan_new")->field('school_name,special_group_name,college_major_name as major_name,batch_name,course_selection_requirements as selection,enrollment_numbers as people_nums,in_school_years as xuezhi')->where($map1)->select();
		//echo Db::name("school_plan_new")->getlastsql();die;
		$major_list_new = [];
		foreach($major_list as $k=>$v){
			$major_list_new[$v['school_name'].$v['special_group_name']][] = $v;
		}
		//dump($major_list);die;
	
		foreach($list as $k=>$v){
			$map['school_name'] = $v['school_name'];
			$map['special_group_name'] = $v['special_group_name'];
			//$list[$k]['major_lst'] = Db::name("school_plan_new")->field('college_major_name as major_name,batch_name,course_selection_requirements as selection,enrollment_numbers as people_nums,in_school_years as xuezhi')->where($map)->page($page,$pagesize)->select();
			//echo Db::name("school_plan_new")->getlastsql();die;
			$list[$k]['major_lst'] = !empty($major_list_new[$v['school_name'].$v['special_group_name']])?$major_list_new[$v['school_name'].$v['special_group_name']]:[];
		}
		
        $data = ['list'=>$list,'count'=>$count,'pages'=>$pages,'pagesize'=>$pagesize];
		return $this->show(1,"成功", $data);
		
    }
	
	//批次线
	public function batch_line()
    {
		
        $province_id = $this->request->get("province_id",0,'intval');
        if(empty($province_id)){
            $province_id = $this->province_id;
        }
        if(empty($province_id)){
            return $this->show(2001,"缺少参数province_id!", '');
        }
		
		$list = [];
		for($i=1;$i<4;$i++){
			$year = date('Y',strtotime("-$i year"));
			$data = Db::name("batch_line")->field('province_id,batch_name,subject_name,socre,score_area')->where('province_id',$province_id)->where('year',$year)->select();
				
					
			$data = array_map(function ($rs){
				$rs['province_title'] = Db::name('area')->where('id',$rs['province_id'])->value('name');
				unset($rs['province_id']);
				return $rs;
			}, $data);
		
		
			$list[] = ['title'=>$year.'批次线','data'=>$data];
		}
		
		
		return $this->show(1,"成功", $list);
		
    }
}